package com.xavier.controller;

import com.xavier.dto.MenuDTO;
import com.xavier.dto.MenuQueryDTO;
import com.xavier.result.Result;
import com.xavier.service.IMenuService;
import com.xavier.vo.MenuDetailVO;
import com.xavier.vo.MenuPageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 请求处理层-菜单管理类
 *
 * @author Xavier
 * @version 1.0
 */
@Slf4j
@RequestMapping("menu")
@RestController
@Tag(name = "菜单管理接口")
public class MenuController extends CommonController {

    @Resource
    private IMenuService menuService;

    @Operation(summary = "获取用户菜单信息", description = "获取当前登录菜单资源，menuList为菜单列表，permissions为权限列表(*.*.*代表管理员权限)")
    @GetMapping("getNav")
    public Result<Map<String, Object>> getUserMenu() {
        return Result.success(menuService.getUserMenu());
    }

    @Operation(summary = "新增菜单", description = "新增菜单")
    @PutMapping("add")
    public Result add(@Validated @RequestBody MenuDTO dto) {
        menuService.createMenu(dto);
        return Result.success();
    }

    @Operation(summary = "编辑菜单", description = "编辑菜单")
    @PutMapping("update")
    public Result update(@Validated @RequestBody MenuDTO dto) {
        menuService.updateMenu(dto);
        return Result.success();
    }

    @Operation(summary = "删除菜单", description = "删除菜单")
    @DeleteMapping("delete/{id}")
    @Parameter(name = "id", description = "id", required = true)
    public Result<Integer> delete(@PathVariable Long id) {
        menuService.deleteMenu(id);
        return Result.success();
    }

    @Operation(summary = "获取菜单信息", description = "根据菜单id获取菜单信息")
    @GetMapping("get/{id}")
    @Parameter(name = "id", description = "id", required = true)
    public Result<MenuDetailVO> get(@PathVariable Long id) {
        return Result.success(menuService.getMenu(id));
    }

    @Operation(summary = "获取菜单树形信息", description = "获取菜单树形信息")
    @PostMapping("getList")
    public Result<List<MenuPageVO>> queryMenu(@Validated @RequestBody MenuQueryDTO dto) {
        return Result.success(menuService.queryMenu(dto));
    }

}
